<?php

/**
 * This is the model class for table "sitemap".
 *
 * The followings are the available columns in table 'sitemap':
 * @property integer $id
 * @property integer $sort
 * @property integer $parent
 * @property string $title
 * @property integer $is_menu_item
 * @property integer $status
 *
 * The followings are the available model relations:
 * @property Page $id0
 * @property Page $parent0
 */
class Sitemap extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'sitemap';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('id', 'required'),
            array('id', 'unique', 'message' => 'Page already in sitemap.'),
            array('id, sort, parent, is_menu_item, status', 'numerical', 'integerOnly' => true),
            array('title', 'length', 'max' => 255),
            array('parent', 'compare', 'compareAttribute' => 'id', 'allowEmpty' => true, 'operator' => '!=', 'message' => 'Parent must not be same.'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, sort, parent, title, is_menu_item, status', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'id0' => array(self::BELONGS_TO, 'Page', 'id'),
            'parent0' => array(self::BELONGS_TO, 'Page', 'parent'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'Page',
            'sort' => 'Sort',
            'parent' => 'Parent',
            'title' => 'Title',
            'is_menu_item' => 'Is Menu Item',
            'status' => 'Status',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('sort', $this->sort);
        $criteria->compare('parent', $this->parent);
        $criteria->compare('title', $this->title, true);
        $criteria->compare('is_menu_item', $this->is_menu_item);
        $criteria->compare('status', $this->status);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Sitemap the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * Url and paging system
     */
    protected function afterSave() {
        parent::afterSave();
        //$pageId=  $this->id;
        $model=new Page();
        //get hierarchy
        $pageAncestors=Tree::getPath($this->id);
        
        $ancestorsList=$model->findAllByAttributes(array('id'=>$pageAncestors));
        $ancestorsUrl=array();
        foreach ($ancestorsList as $ancestor){
            $ancestorsUrl[]=$ancestor->getUrl();
            
        }
        // update page url with defined hirarchy
        $page=$model->find("id=".$this->id);
        if(count($ancestorsUrl)>0)
        $page->slug=  implode(DIRECTORY_SEPARATOR, $ancestorsUrl).DIRECTORY_SEPARATOR.$page->getUrl();
        else
            $page->slug=$page->getUrl();
      //  if($page->id==$this->id)
        $page->save();
    }

}
